<?php
    class VentePrivee {
        //Alias sur table : vp
        public $id_vente_privee;
        public $libelle;
        public $date_debut;
        public $date_fin;

        //Constructeur complet
        public function __construct($id_vente_privee, $libelle, $date_debut, $date_fin) {
            $this->setId($id_vente_privee);
            $this->setLibelle($libelle);
            $this->setDateDebut($date_debut);
            $this->setDateFin($date_fin);
        }

        //Ajoute un objet VentePrivee à la BDD
        //Paramètre : aucun
        //Retourne (VentePrivee)
        public function add() {
            $sql =  'INSERT INTO vente_privee (libelle, date_debut, date_fin) VALUES
                    ("'.Db::insertion($this->libelle).'", "'.$this->getDateDebut().'", "'.$this->getDateFin().'")';
            $this->setId(Db::Execute($sql));
            return $this;
        }
        
        //Supprime le produit de la vente privee
        //Paramètre : aucun
        //Retourne (Produit)
        public function suppr() {
            $sql =  'UPDATE produit
                    SET vente_privee = 0;
                    WHERE
                        id_produit = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
        
        
        //Modifie un objet VentePrivee dans la BDD
        //Paramètre : aucun
        //Retourne (VentePrivee)
        public function update() {
            $sql =  'UPDATE vente_privee
                    SET libelle = "'.Db::insertion($this->libelle).'",
                        date_debut = "'.$this->getDateDebut().'", 
                        date_fin = "'.$this->getDateFin().'"
                    WHERE
                        id_vente_privee = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
        
        //Récupère tout les VentePriveeDetail de cette VentePrivee
        //Paramètre : aucun
        //Retourne collection de VentePriveeDetail
        public function getVentePriveeDetails() {
            $sql =  'SELECT
                        vpd.id_vente_privee_detail as vpdid_vente_privee_detail, vpd.quantite_restante as vpdquantite_restante, 
                        vpd.promotion as vpdpromotion,
                        tv.id_tva as tvid_tva, tv.taux as tvtaux, 
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide, 
                        prod.id_produit as prodid_produit, prod.libelle as prodlibelle, prod.reference as prodreference, prod.description as proddescription,
                        prod.description_longue as proddescription_longue, prod.prix_unit as prodprix_unit, prod.promotion as prodpromotion, 
                        prod.vente_privee as prodvente_privee, prod.date_ajout as proddate_ajout, prod.date_modif as proddate_modif, 
                        prod.valide as prodvalide, prod.stock as prodstock
                    FROM vente_privee_detail vpd
                    LEFT JOIN produit prod ON vpd.id_produit = prod.id_produit
                    LEFT JOIN tva tv ON prod.id_tva = tv.id_tva
                    LEFT JOIN marque ma ON prod.id_marque = ma.id_marque
                    WHERE
                        vpd.id_vente_privee = '.intval($this->getId());
            $tabRetour = array();
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $marque = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'],
                                                        $p['madate_modif'], $p['mavalide']);
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                $produit = new Produit($p['prodid_produit'], $marque, $p['prodlibelle'], $p['prodreference'], $p['proddescription'], 
                                    $p['proddescription_longue'], $p['prodstock'], $p['prodprix_unit'], $tva, $p['prodpromotion'], $p['prodvente_privee'], 
                                    $p['proddate_ajout'], $p['proddate_modif'], $p['prodvalide']);
                $tabRetour[] = new VentePriveeDetail($p['vpdid_vente_privee_detail'], $this, $produit, $p['vpdquantite_restante'], 
                                                    $p['vpdpromotion']);
            }
            return $tabRetour;
        }
        
        //Récupère toutes les VentePrivee en cours aujourd'hui
        //Paramètre : OPTIONNEL (Booléen)actuel, (Booléen)all
        //Retourne collection de VentePrivee
        public static function getVentePrivees($actuel = true, $all = false) {
            $ajd = new DateTime();
            $sql =  'SELECT
                        vp.id_vente_privee as vpid_vente_privee, vp.libelle as vplibelle, vp.date_debut as vpdate_debut, 
                        vp.date_fin as vpdate_fin
                    FROM vente_privee vp';
            if(!$all) {
                $sql .= ' WHERE';
                if($actuel) {
                    $sql .= ' vp.date_debut <= "'.$ajd->format('Y-m-d').'"
                            AND vp.date_fin >= "'.$ajd->format('Y-m-d').'"
                            AND vp.date_fin <> "0000-00-00"';
                }
                else {
                    $sql .= ' vp.date_debut >= "'.$ajd->format('Y-m-d').'"
                            AND vp.date_fin >= "'.$ajd->format('Y-m-d').'"
                            AND vp.date_fin <> "0000-00-00"';
                }
            } 
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $tabRetour[] = new VentePrivee($p['vpid_vente_privee'], $p['vplibelle'], $p['vpdate_debut'], $p['vpdate_fin']);
            }
            return $tabRetour;
        }
        
        //Recherche tous les détails d'un produit en vente privée
        //Paramètre : (int)id
        //Retourne (VentePrivee)
        public static function rechercheVentePrivee($id) {
            $sql =  'SELECT
                        vp.id_vente_privee as vpid_vente_privee, vp.libelle as vplibelle, vp.date_debut as vpdate_debut, 
                        vp.date_fin as vpdate_fin
                    FROM vente_privee vp
                    WHERE
                        vp.id_vente_privee = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                return new VentePrivee($p['vpid_vente_privee'], $p['vplibelle'], $p['vpdate_debut'], $p['vpdate_fin']);
            }
            return null;
        }
        
        //GETTERS ET SETTERS
        public function getId() {return $this->id_vente_privee;}
        public function getLibelle() {return $this->libelle;}
        public function getDateDebut($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_debut, $format);}
        public function getDateFin($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_fin, $format);}

        public function setId($temp) {$this->id_vente_privee = intval($temp);}
        public function setLibelle($temp) {$this->libelle = $temp;}
        public function setDateDebut($temp) {$this->date_debut = Tools::setterDate($temp);}
        public function setDateFin($temp) {$this->date_fin = Tools::setterDate($temp);}
    }

?>